package com.condominio.servlet.areacomun;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.condominio.excepcion.DAOExcepcion;
import com.condominio.modelo.Persona;
import com.condominio.negocio.Alquiler_AreaComunNegocio;

/**
 * Servlet implementation class AreaComunInsertarAlquilerServlet
 */
@WebServlet("/AreaComunInsertarAlquilerServlet")
public class AreaComunInsertarAlquilerServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AreaComunInsertarAlquilerServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		Persona oUsuario = (Persona)session.getAttribute("USUARIO_ACTUAL");
		
		if(oUsuario == null){
			request.getRequestDispatcher("error.jsp").forward(request, response);
			return;
		}
		
		int CoResidente = Integer.parseInt(request.getParameter("cbresidente"));
		int coArea = Integer.parseInt(request.getParameter("cbareacomun"));
		String FeInicioAlquiler  = request.getParameter("cbanioinicio") + "-"  + request.getParameter("cbmesinicio") + "-" + request.getParameter("cbdiainicio");
		String FeFinAlquiler  =request.getParameter("cbaniofinal") + "-"  + request.getParameter("cbmesfinal") + "-" + request.getParameter("cbdiafinal");
		double cuota = Double.parseDouble(request.getParameter("txtcuota"));
			
		Alquiler_AreaComunNegocio negocio = new Alquiler_AreaComunNegocio();
		try {
			negocio.InsertaAlquilerAreaComun(CoResidente, coArea, FeInicioAlquiler, FeFinAlquiler, cuota, "1");
			response.sendRedirect(request.getContextPath() + "/ListarAlquilerAreaComunServlet");
		} catch (DAOExcepcion e) {
			session.setAttribute("ERROR_MSG", e.getMessage());
			RequestDispatcher rd = request.getRequestDispatcher("AreaComunCallAlquilerAreaServlet");   //error.jsp
			rd.forward(request, response);
		}
	}
		
}
